package jp.com.nobnak.commentviewer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jp.com.nobnak.commentviewer.common.IAction1;
import jp.com.nobnak.commentviewer.provider.LiveStatusContract;
import jp.com.nobnak.commentviewer.util.UriCommandLauncher;
import jp.com.nobnak.commentviewer.util.WebViewUtil;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;

public class ThreadChooserActivity extends Activity {
	static public final String LOG_TAG = "CommentViewerActivity";
	
	
	private WebView webView;	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        CookieSyncManager.createInstance(this);
        
        webView = (WebView) findViewById(R.id.login_webview);

        UriCommandLauncher launcher = new UriCommandLauncher();
		IAction1<Matcher> action = new IAction1<Matcher>() {
			@Override
			public void call(Matcher param1) {
				webView.loadUrl(param1.group());
			}
		};

		launcher.addCommand(Pattern.compile(
				getString(R.string.NICO_LOGIN_URI)), action);
		launcher.addCommand(Pattern.compile(getString(R.string.NICO_LIVE_CONTENT)), 
				new IAction1<Matcher>() {
					@Override
					public void call(Matcher param1) {
//						LiveStatusService live = new LiveStatusService(
//								"live.nicovideo.jp", "/", 
//								String.format("http://live.nicovideo.jp/api/getplayerstatus?v=%s",
//										param1.group(1)));
//						live.listenResult(new IAction1<IResult>() {
//							@Override
//							public void call(IResult param1) {
//								ILiveStatus status = param1.getLiveStatus();
//								Log.d(LOG_TAG, status.getMessageServerThread());
//							}
//						});
//						live.listenError(new IAction1<IError>() {
//							@Override
//							public void call(IError param1) {
//								Toast.makeText(ThreadChooserActivity.this, param1.getMessage(), 
//										Toast.LENGTH_SHORT).show();
//							}
//						});
//						live.send();
						Uri liveUri = Uri.withAppendedPath(
								LiveStatusContract.CONTENT_LIVE_URI, param1.group(1));
						Intent commentFeeder = new Intent(
								ThreadChooserActivity.this, CommentFeedActivity.class);
						startActivity(commentFeeder);
					}
				});
		launcher.addCommand(Pattern.compile(
				getString(R.string.NICO_LIVE_URI)), action);
        WebViewUtil.initWebView(webView, this, launcher);
    }

	@Override
	public void onBackPressed() {
		if (webView.canGoBack()) {
			webView.goBack();
			return;
		}
		super.onBackPressed();
	}

	@Override
	protected void onResume() {
		super.onResume();
		CookieSyncManager.getInstance().startSync();
		
		webView.loadUrl("http://live.nicovideo.jp/");
	}
	@Override
	protected void onPause() {
		CookieSyncManager.getInstance().stopSync();
		super.onPause();
	}

}